#ifndef __CAN_H
#define __CAN_H

#include "../../imx6ul/imx6ul.h"
#include "../../stdio/include/stdio.h"
#include "../gpio/bsp_gpio.h"
#include "../int/bsp_int.h"
#include "stdbool.h"
#include <stdint.h>

typedef struct _mb_params_t {
  int8_t dlc;       // 数据长度
  int32_t frame_id; // can帧的id
  int32_t word0;    // 数据0 低4个字节
  int32_t word1;    // 数据1 高4个字节
} mb_params_t;

/*! @brief FlexCAN message frame structure. */
typedef struct _can_frame {
  uint8_t index;     // 从那接收数据
  uint8_t size;      // 数据长度
  uint8_t *buf;      // 数据
  uint32_t frame_id; // can帧的id
} can_frame_t;

extern volatile int8_t read_data_flag;

void can_gpio_init(void);
void can_controller_init(CAN_Type *base);
void can_protocol_timing_init(CAN_Type *base);
void can_tx_mb_config(CAN_Type *base, uint8_t mb_index, mb_params_t *mb_params);
void can_rx_mb_config(CAN_Type *base, uint8_t mb_index, int32_t can_id);
void can_send_data(CAN_Type *base, can_frame_t *can_frame);
uint8_t can_read_data(CAN_Type *base, can_frame_t *can_frame);

void can_interrupt_init(CAN_Type *base, can_frame_t *can_frame);
#endif